from django.db import models

from player.models import OCBPlayer, Ally
from constants import RACES_STRING, PROD_TYPES, GOV_SYSTEMS, STASIS_CHOICES

# Create your models here.
class Planet(models.Model):
    name = models.CharField("Planet name",max_length=50)
    hyperiums_id = models.IntegerField("Hyperiums id",unique=True)
    gouv_system = models.IntegerField("Gouvernment system")
    race = models.IntegerField("Race")
    prod_type = models.IntegerField("Production type")
    x = models.IntegerField("X coord")
    y = models.IntegerField("Y coord")
    sc = models.IntegerField("SC")
    ally = models.ForeignKey(Ally,blank=True,null=True)
    activity = models.IntegerField("Activity")
    previous_name = models.CharField("Planet name",max_length=50,blank=True,null=True)

    def __repr__(self):
        return self.name
    
    def __str__(self):
        return self.name

    def str_tag(self):
        return self.ally.alliance_tag

    def get_gouv_system(self):
        return GOV_SYSTEMS[self.gouv_system]
    
    def get_race(self):
        return RACES_STRING[self.race]
    
    def get_prod_type(self):
        return PROD_TYPES[self.prod_type]
    
    str_gouv_system = property(get_gouv_system)
    str_race = property(get_race)
    str_prod_type = property(get_prod_type)
    tag = property(str_tag)
    
    def get_bullet(self):
        if self.prod_type == 0 :
            return "url('/media/img/bullet_green.png')"
        elif self.prod_type == 1 :
            return "url('/media/img/bullet_red.png')"
        elif self.prod_type == 2 :
            return "url('/media/img/bullet_blue.png')"


    def get_color(self):
        print PlanetInfo.objects.filter(planet=self).count()
        if PlanetInfo.objects.filter(planet=self).count() == 0:
            return "safe"
        elif (self.planetinfo_set.all()[0].planet_owner in OCBPlayer.objects.all()) or self.planetinfo_set.all()[0].is_neutral :
            if self.staticfleet_set.filter(defending=False) != [] :
                return "safe"
            else :
                attacking_fleet = sum([fleet.amount_fleet for fleet in self.staticfleet_set.filter(defending=False)])
                defending_fleet = sum([fleet.amount_fleet for fleet in self.staticfleet_set.filter(defending=True)])
                if attacking_fleet > defending_fleet :
                    return "notcovered"
                else :
                    return "covered"
        else :
            if self.gouv_system == 0:
                return "retakedict"
            elif self.gouv_system == 3:
                return "hyp"
            else :
                return "retake"


class PlanetInfo(models.Model):
    planet = models.ForeignKey(Planet)
    planet_owner = models.ForeignKey(OCBPlayer,null=True,blank=True,related_name="ocb_owner")
    planet_survey = models.ManyToManyField(OCBPlayer,null=True,blank=True,related_name="ocb_guardian")
    energy = models.IntegerField("Energy ",null=True,blank=True)
    max_energy = models.IntegerField("Max Energy ",null=True,blank=True)
    stasis = models.IntegerField("Stasis",choices=STASIS_CHOICES,default=3)
    last_update_date = models.DateTimeField("Last Update",null=True,blank=True)
    is_neutral = models.BooleanField("Neutral ",default=False)
    
    def __repr__(self):
        return self.planet.name
    
    def __str__(self):
        return self.planet.name

class PlanetMessage(models.Model):
    planet = models.ForeignKey(Planet)
    date = models.DateTimeField("Date")
    message = models.CharField("Event",max_length=250)
    class Meta:
        unique_together = ('planet', 'date','message')
